package leetcode_day._2021._202109._1120;

import java.util.HashMap;
import java.util.Map;

/**
 * @author yzh
 * @version 1.0
 * @date 2021/9/13 8:52
 * 回旋镖的数量
 * 算法：哈希表  数组
 * 计算出两两之间的距离
 */
public class _13_447 {

    public int numberOfBoomerangs(int[][] points) {
        int len = points.length;
        if (len < 3) return 0;
        int ans = 0;
        for (int i = 0; i < len; i++) {
            Map<Integer, Integer> map = new HashMap<>(len);
            for (int j = 0; j < len; j++) {
                if (i == j) continue;
                int dis = distance(points[i], points[j]);
                if (map.containsKey(dis)) ans += map.get(dis) * 2;
                map.put(dis, map.getOrDefault(dis, 0) + 1);
            }
        }
        return ans;
    }
    int distance(int[] point1, int[] point2) {
        int x = (int) Math.pow(point1[0] - point2[0], 2);
        int y = (int) Math.pow(point1[1] - point2[1], 2);
        return x + y;
    }

}
